package cn.tedu.yl.role.service.impl;

import cn.tedu.yl.base.exception.ServiceException;
import cn.tedu.yl.base.response.PageData;
import cn.tedu.yl.base.response.StatusCode;
import cn.tedu.yl.role.mapper.RoleMapper;
import cn.tedu.yl.role.pojo.dto.RoleDelParam;
import cn.tedu.yl.role.pojo.dto.RoleListQuery;
import cn.tedu.yl.role.pojo.dto.RoleSaveParam;
import cn.tedu.yl.role.pojo.entity.Role;
import cn.tedu.yl.role.pojo.vo.RoleVO;
import cn.tedu.yl.role.service.roleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class roleServiceImpl implements roleService {
    @Autowired
    private RoleMapper roleMapper;

    //分页查询列表数据
    @Override
    public PageData selectList(RoleListQuery roleListQuery) {
        log.info("获取角色管理分页列表数据参数:{}",roleListQuery);
        List<RoleVO> list = roleMapper.selectList(roleListQuery);
        int total = roleMapper.selectListCount(roleListQuery);
        PageData pageData = new PageData();
        pageData.setList(list);
        pageData.setTotal(total);

        return pageData;
    }


    //编辑角色
    @Override
    public void save(RoleSaveParam roleSaveParam) {
        log.info("获取角色编辑参数:{}",roleSaveParam);
        Role role = new Role();
        BeanUtils.copyProperties(roleSaveParam,role);

        if(roleSaveParam.getId() == null){
            //新增
            log.info("进入新增角色模块");
            role.setCreateTime(new Date());
            roleMapper.insert(role);
        } else {
            //编辑
            log.info("进入编辑角色模块");
            role.setUpdateTime(new Date());
            roleMapper.update(role);
        }
    }

    //批量删除
    @Override
    public void delete(RoleDelParam roleDelParam) {
        List<Integer> ids = roleDelParam.getArrIds();
        log.info("获取角色管理批量删除参数,数组ids:{}",ids);
        roleMapper.delete(ids);
    }


    //根据id查询详情信息
    @Override
    public RoleVO selectDetailById(Integer id) {
        RoleVO roleVO = roleMapper.selectDetailById(id);
        return roleVO;
    }


}

































